package org.example;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveAction;
import java.util.stream.Collectors;

public class LinkTask extends RecursiveAction {
    private final LinkList list;
    private final URL url;
    private final int deepLimit;

    public LinkTask(LinkList list, URL url, int deepLimit) {
        this.list = list;
        this.url = url;
        this.deepLimit = deepLimit;
    }

    @Override
    protected void compute() {
        if (!list.add(url)) {
            return;
        }

        List<ForkJoinTask<Void>> childTasks = new ArrayList<>();

        list.connectionDelay();
        System.out.print(".");

        try {
            Document document = Jsoup.connect(url.toString()).get();

            if (deepLimit != 0) {
                childTasks.addAll(
                        document.select("a[href]").stream()
                        .map(element -> element.attr("abs:href"))
                        .map(link -> link.replaceAll("#.*", ""))
                        .map(link -> link.replaceAll("/+$", ""))
                        .map(link -> {
                            try {
                                return new URL(link);
                            } catch (MalformedURLException ignored) {}
                            return null;
                        })
                        .filter(list::isRootDomain)
                        .map(url -> new LinkTask(list, url, deepLimit - 1).fork())
                        .collect(Collectors.toList())
                );
            }
        } catch (IOException e) {
            list.remove(url);
        }

        childTasks.forEach(ForkJoinTask::join);
    }
}
